<?php

/**
 * 后台Action
 * Some rights reserved：abc3210.com
 * Contact email:admin@abc3210.com
 */
class AdminbaseAction extends AppframeAction {

    function _initialize() {
        //定义是后台
        define('IN_ADMIN',true);
        parent::_initialize();
        //初始化当前登录用户信息
        $this->initUser();
        //初始化模型
        $this->initModel();
        //角色表名称
        C("RBAC_ROLE_TABLE", C("DB_PREFIX") . "role");
        //用户表名称
        C("RBAC_USER_TABLE", C("DB_PREFIX") . "role_user");
        //权限表名称
        C("RBAC_ACCESS_TABLE", C("DB_PREFIX") . "access");
        //节点表名称
        C("RBAC_NODE_TABLE", C("DB_PREFIX") . "node");
        //后台用户模型
        C("USER_AUTH_MODEL", "User");
        
        C("USER_AUTH_GATEWAY", U("Admin/Public/login"));

        if (!RBAC::AccessDecision(GROUP_NAME)) {
            //检查认证识别号
            if (!$_SESSION [C('USER_AUTH_KEY')]) {
                //跳转到认证网关
                $this->assign("jumpUrl",C('USER_AUTH_GATEWAY'));
                $this->error("请登陆后操作！");
                //redirect(C('USER_AUTH_GATEWAY'));
                exit;
            }
            // 没有权限 抛出错误
            if (C('RBAC_ERROR_PAGE')) {
                // 定义权限错误页面
                redirect(C('RBAC_ERROR_PAGE'));
            } else {
                // 提示错误信息
                $this->error(L('_VALID_ACCESS_'));
            }
        }
        $this->initMenu();
    }
    
    /**
     * 消息提示
     * @param type $message
     * @param type $jumpUrl
     * @param type $ajax 
     */
    public function success($message, $jumpUrl = '', $ajax = false) {
        parent::success($message, $jumpUrl, $ajax);
        $text = "应用：".GROUP_NAME.",模块：".MODULE_NAME.",方法：".ACTION_NAME."<br>提示语：".$message;
        $this->addLogs($text);
    }

    /**
     * 初始化后台菜单
     */
    private function initMenu() {
        $Menu = F("Menu");
        if (!$Menu) {
            $Menu = D("Menu")->Menucache();
            //数组
            $eMenu = D("Menu")->select();
            foreach ($eMenu as $k => $v) {
                $data[$v['id']] = $v;
            }
            F("eMenu", $data);
        }
        $this->Cache['Menu'] = $Menu;
        $this->Cache['eMenu'] = F("eMenu");
    }

    /**
     *  添加
     */
    protected function add() {
        $this->display();
    }

    protected function insert($model) {
        if (!is_object($model)) {
            return false;
        }
        if ($model->create()) {
            if ($model->add()) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     *  编辑
     */
    protected function edit($model) {
        if (!is_object($model)) {
            return false;
        }
        $pk = ucfirst($model->getPk()); //取得主键
        $id = (int) $_REQUEST[$model->getPk()];
        if (empty($id)) {
            return false;
        }
        $do = 'getBy' . $pk;
        $vo = $model->$do($id); //TP 5.3.12
        return $vo;
    }

    protected function updata($model) {
        if (!is_object($model)) {
            return false;
        }
        if ($model->create()) {
            if ($model->save()) {
                return true;
            } else {
                $this->error("更新失败！");
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     *  删除
     *  通过getPk获取表的主键，根据主键名称取得(post/get)的值，去删除数据库中的记录
     */
    protected function delete($model) {
        if (!is_object($model)) {
            return false;
        }
        $pk = $model->getPk(); //获取主键名称
        $id = (int) $_REQUEST[$pk];
        if (isset($id)) {
            if (false !== $model->delete($id)) {
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     *  排序 排序字段为listorders数组 POST 排序字段为：listorder
     */
    protected function listorders($model) {
        if (!is_object($model)) {
            return false;
        }
        $pk = $model->getPk(); //获取主键名称
        $ids = $_POST['listorders'];
        foreach ($ids as $key => $r) {
            $data['listorder'] = $r;
            $model->where(array($pk => $key))->save($data);
        }
        return true;
    }

    protected function page($Total_Size = 1, $Page_Size = 0, $Current_Page = 1, $listRows = 6, $PageParam = '', $PageLink = '', $Static = FALSE) {
        import('@.ORG.Util.Page');
        if ($Page_Size == 0) {
            $Page_Size = C("PAGE_LISTROWS");
        }
        if (empty($PageParam)) {
            $PageParam = C("VAR_PAGE");
        }
        $Page = new Page($Total_Size, $Page_Size, $Current_Page, $listRows, $PageParam, $PageLink, $Static);
        $Page->SetPager('Admin', '共有{recordcount}条信息&nbsp;{pageindex}/{pagecount}&nbsp;{first}{prev}&nbsp;{liststart}{list}{listend}&nbsp;{next}{last}&nbsp;转到{jump}页', array("listlong" => "6", "first" => "首页", "last" => "尾页", "prev" => "上一页", "next" => "下一页", "list" => "*", "disabledclass" => ""));
        return $Page;
    }

}

?>
